package com.vay.de.dang.online.view;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import com.vay.de.dang.online.R;
import com.vay.de.dang.online.entiy.MenuItemEntity;
import com.vay.de.dang.online.util.VnUtil;
import com.vay.de.dang.online.web.ReqCallback;
import com.vay.de.dang.online.web.ReqResult;
import com.vay.de.dang.online.web.TableApi;

public class DialogMenuOption extends DialogBase {

    private RelativeLayout parentLayout;
    private LinearLayout itemLayout;
    private OnOptionItemClick itemClick;
    private TextView titleTv;
    private Activity activity;

    public DialogMenuOption(Context context) {
        super(context);
        super.setContentView(R.layout.dialog_menu_option);
        autoWidth(1.0f);
        parentLayout = findViewById(R.id.parent_layout);
        itemLayout = findViewById(R.id.item_layout);
        titleTv = findViewById(R.id.title_tv);
        activity = (Activity) context;
        findViewById(R.id.bnt_close).setOnClickListener(view -> {
            dismiss();
        });
    }

    public DialogMenuOption setOnOptionItemClick(OnOptionItemClick click) {
        itemClick = click;
        return this;
    }

    public void showChoice(String title, String codeType) {
        if (VnUtil.isEmpty(codeType)) return;
        titleTv.setText(title);
        TableApi.codeTable(codeType, new ReqCallback(activity) {
            @Override
            public void callback(ReqResult value) {
                if (value.isOk()) {
                    loadData((List<MenuItemEntity>) value.result);
                }
            }
        }.showDialog(true));
    }

    public void showList(String title, List<MenuItemEntity> list) {
        if (VnUtil.isEmpty(list)) return;
        titleTv.setText(title);
        loadData(list);
    }

    private void loadData(List<MenuItemEntity> list) {
        itemLayout.removeAllViews();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        int size = list.size();
        for (int index = 0; index < list.size(); index++) {
            MenuItemEntity entity = list.get(index);
            View iv = inflater.inflate(R.layout.item_menu, null);
            itemLayout.addView(iv);

            TextView tv = iv.findViewById(R.id.name_tv);
            tv.setText(entity.getLabel());

            iv.setOnClickListener(view -> {
                dismiss();
                if (itemClick != null) {
                    itemClick.onOptionItemClick(entity);
                }
            });
        }

        DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
        if (size == 2) {
            parentLayout.getLayoutParams().height = (int) (dm.heightPixels * 0.20f);
        } else if (size == 3) {
            parentLayout.getLayoutParams().height = (int) (dm.heightPixels * 0.25f);
        } else if (size == 4) {
            parentLayout.getLayoutParams().height = (int) (dm.heightPixels * 0.30f);
        } else if (size <= 5) {
            parentLayout.getLayoutParams().height = (int) (dm.heightPixels * 0.35f);
        } else {
            parentLayout.getLayoutParams().height = (int) (dm.heightPixels * 0.45f);
        }

        show();
    }

    public interface OnOptionItemClick {

        void onOptionItemClick(MenuItemEntity bean);
    }
}
